//
//  MainDetailTopPropertyTbv.m
//  ZITHER
//
//  Created by weixin on 2018/8/30.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainDetailTopPropertyTbv.h"
#import "MainDetailTopPropertyCell.h"

@implementation MainDetailTopPropertyTbv

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor randomColor];

        self.delegate = self;
        self.dataSource = self;
        self.bounces = NO;//这里设置禁用会导致卡主无法滑动
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.rowHeight = UITableViewAutomaticDimension;
        self.estimatedRowHeight = 5;
        [self registerNib:[UINib nibWithNibName:@"MainDetailTopPropertyCell" bundle:nil] forCellReuseIdentifier:@"MainDetailTopPropertyCell"];
        
    }
    return self;
}



#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    attributesModel *model = self.model.attributes[section];
    return model.categoryValue.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.model.attributes.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* TODO: ================= 判断 cell 样式 =================*/
    
    static NSString *sectionCell = @"MainDetailTopPropertyCell";
    MainDetailTopPropertyCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
    
    if (cell == nil) {
        cell = [[MainDetailTopPropertyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }

    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    attributesModel *model = self.model.attributes[indexPath.section];
 
    cell.model = model.categoryValue[indexPath.row];
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
    
}



//设置表头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

//添加标头中的内容
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    attributesModel *model = self.model.attributes[section];

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 40)];
    
    UIView *linView = [[UIView alloc]initWithFrame:CGRectMake(7, 22, 4, 16)];
    linView.backgroundColor = HexColor(0x333333);
    [view addSubview:linView];
    
    QMUILabel *leb = [[QMUILabel alloc]initWithFrame:CGRectMake(25,22, kScreenW, 15)];
    leb.text = model.categoryName;
    leb.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];

    
    
    [view addSubview:leb];
    return view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self)
    {
        CGFloat sectionHeaderHeight = 60;
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}


-(void)setModel:(skuDetailModel *)model{
    _model = model;
    [self reloadData];
}

@end
